class Graph{
    constructor(isDirected=false){
        this.isDirected = isDirected;  //是否是有向图
        this.vertexes = [];//所有的顶点
        this.adjList = new Map();  //类似对象的数据结构,键可以用任何类型表示  set()  get();    {a:'sdsd'}
    }

    //添加顶点
    addVertex(vertex){
        if(!this.vertexes.includes(vertex)){
            this.vertexes.push(vertex);
            this.adjList.set(vertex,[]);
        }
    }
    
    //添加边
    addEdge(w,v){
        if(!this.vertexes.includes(w)){
            this.addVertex(w)
        }

        if(!this.vertexes.includes(v)){
            this.addVertex(v)
        }

        this.adjList.get(w).push(v);
        if(!this.isDirected){
            this.adjList.get(v).push(w);
        }
    }

    getVertex(){
        return this.vertexes
    }

    getList(){
        return this.adjList
    }

    toString(){
        let {length} = this.vertexes;

        let str = '';

        for(let i = 0;i<length;i++){
            let vertex = this.vertexes[i]; //A  B  C

            let list = this.adjList.get(vertex); //[B,C]

            str += `${vertex}--->`;

            for(let j = 0;j<list.length;j++){
                str+= `${list[j]}`
            }

            str += '\n';

        }

        return str

    }
}

let g = new Graph(true);

g.addVertex(0);

g.addEdge(0,5);

g.addEdge(0,1);

g.addEdge(2,0);

g.addEdge(2,3);

console.log(g.getVertex());

console.log(g.getList());

console.log(g.toString());

/**
 * A ---> B C
 * 
 * B----> A
 * 
 * C---->A
 * 
*/